home *** CD-ROM | disk | FTP | other *** search
/ Aminet 1 (Walnut Creek) / Aminet - June 1993 [Walnut Creek].iso / aminet / util / gnu / emacs_src_18_58.lha / emacs-18.58 / src / regex.c < prev    next >
C/C++ Source or Header  |  1992-04-26  |  44KB  |  1,716 lines

  1. /* Extended regular expression matching and search.
  2.    Copyright (C) 1985 Free Software Foundation, Inc.
  3.  
  4.     This program is free software; you can redistribute it and/or modify
  5.     it under the terms of the GNU General Public License as published by
  6.     the Free Software Foundation; either version 1, or (at your option)
  7.     any later version.
  8.  
  9.     This program is distributed in the hope that it will be useful,
  10.     but WITHOUT ANY WARRANTY; without even the implied warranty of
  11.     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12.     GNU General Public License for more details.
  13.  
  14.     You should have received a copy of the GNU General Public License
  15.     along with this program; if not, write to the Free Software
  16.     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  17.  
  18. In other words, you are welcome to use, share and improve this program.
  19. You are forbidden to forbid anyone else to use, share and improve
  20. what you give them.   Help stamp out software-hoarding!  */
  21.  
  22.  
  23. /* To test, compile with -Dtest.
  24.  This Dtestable feature turns this into a self-contained program
  25.  which reads a pattern, describes how it compiles,
  26.  then reads a string and searches for it.  */
  27.  
  28.  
  29. #ifdef emacs
  30.  
  31. /* The `emacs' switch turns on certain special matching commands
  32.  that make sense only in emacs. */
  33.  
  34. #include "config.h"
  35. #include "lisp.h"
  36. #include "buffer.h"
  37. #include "syntax.h"
  38.  
  39. #else  /* not emacs */
  40.  
  41. /*
  42.  * Define the syntax stuff, so we can do the \<...\> things.
  43.  */
  44.  
  45. #ifndef Sword /* must be non-zero in some of the tests below... */
  46. #define Sword 1
  47. #endif
  48.  
  49. #define SYNTAX(c) re_syntax_table[c]
  50.  
  51. #ifdef SYNTAX_TABLE
  52.  
  53. char *re_syntax_table;
  54.  
  55. #else
  56.  
  57. static char re_syntax_table[256];
  58.  
  59. static void
  60. init_syntax_once ()
  61. {
  62.    register int c;
  63.    static int done = 0;
  64.  
  65.    if (done)
  66.      return;
  67.  
  68.    bzero (re_syntax_table, sizeof re_syntax_table);
  69.  
  70.    for (c = 'a'; c <= 'z'; c++)
  71.      re_syntax_table[c] = Sword;
  72.  
  73.    for (c = 'A'; c <= 'Z'; c++)
  74.      re_syntax_table[c] = Sword;
  75.  
  76.    for (c = '0'; c <= '9'; c++)
  77.      re_syntax_table[c] = Sword;
  78.  
  79.    done = 1;
  80. }
  81.  
  82. #endif /* SYNTAX_TABLE */
  83. #endif /* not emacs */
  84.  
  85. #include "regex.h"
  86.  
  87. /* Number of failure points to allocate space for initially,
  88.  when matching.  If this number is exceeded, more space is allocated,
  89.  so it is not a hard limit.  */
  90.  
  91. #ifndef NFAILURES
  92. #define NFAILURES 80
  93. #endif NFAILURES
  94.  
  95. /* width of a byte in bits */
  96.  
  97. #define BYTEWIDTH 8
  98.  
  99. #ifndef SIGN_EXTEND_CHAR
  100. #define SIGN_EXTEND_CHAR(x) (x)
  101. #endif
  102.  
  103. static int obscure_syntax = 0;
  104.  
  105. /* Specify the precise syntax of regexp for compilation.
  106.    This provides for compatibility for various utilities
  107.    which historically have different, incompatible syntaxes.
  108.  
  109.    The argument SYNTAX is a bit-mask containing the two bits
  110.    RE_NO_BK_PARENS and RE_NO_BK_VBAR.  */
  111.  
  112. int
  113. re_set_syntax (syntax)
  114. {
  115.   int ret;
  116.  
  117.   ret = obscure_syntax;
  118.   obscure_syntax = syntax;
  119.   return ret;
  120. }
  121.  
  122. /* re_compile_pattern takes a regular-expression string
  123.    and converts it into a buffer full of byte commands for matching.
  124.  
  125.   PATTERN   is the address of the pattern string
  126.   SIZE      is the length of it.
  127.   BUFP        is a  struct re_pattern_buffer *  which points to the info
  128.         on where to store the byte commands.
  129.         This structure contains a  char *  which points to the
  130.         actual space, which should have been obtained with malloc.
  131.         re_compile_pattern may use  realloc  to grow the buffer space.
  132.  
  133.   The number of bytes of commands can be found out by looking in
  134.   the  struct re_pattern_buffer  that bufp pointed to,
  135.   after re_compile_pattern returns.
  136. */
  137.  
  138. #define PATPUSH(ch) (*b++ = (char) (ch))
  139.  
  140. #define PATFETCH(c) \
  141.  {if (p == pend) goto end_of_pattern; \
  142.   c = * (unsigned char *) p++; \
  143.   if (translate) c = translate[c]; }
  144.  
  145. #define PATFETCH_RAW(c) \
  146.  {if (p == pend) goto end_of_pattern; \
  147.   c = * (unsigned char *) p++; }
  148.  
  149. #define PATUNFETCH p--
  150.  
  151. #define EXTEND_BUFFER \
  152.   { char *old_buffer = bufp->buffer; \
  153.     if (bufp->allocated == (1<<16)) goto too_big; \
  154.     bufp->allocated *= 2; \
  155.     if (bufp->allocated > (1<<16)) bufp->allocated = (1<<16); \
  156.     if (!(bufp->buffer = (char *) realloc (bufp->buffer, bufp->allocated))) \
  157.       goto memory_exhausted; \
  158.     c = bufp->buffer - old_buffer; \
  159.     b += c; \
  160.     if (fixup_jump) \
  161.       fixup_jump += c; \
  162.     if (laststart) \
  163.       laststart += c; \
  164.     begalt += c; \
  165.     if (pending_exact) \
  166.       pending_exact += c; \
  167.   }
  168.  
  169. static int store_jump (), insert_jump ();
  170.  
  171. char *
  172. re_compile_pattern (pattern, size, bufp)
  173.      char *pattern;
  174.      int size;
  175.      struct re_pattern_buffer *bufp;
  176. {
  177.   register char *b = bufp->buffer;
  178.   register char *p = pattern;
  179.   char *pend = pattern + size;
  180.   register unsigned c, c1;
  181.   char *p1;
  182.   unsigned char *translate = (unsigned char *) bufp->translate;
  183.  
  184.   /* address of the count-byte of the most recently inserted "exactn" command.
  185.     This makes it possible to tell whether a new exact-match character
  186.     can be added to that command or requires a new "exactn" command. */
  187.      
  188.   char *pending_exact = 0;
  189.  
  190.   /* address of the place where a forward-jump should go
  191.     to the end of the containing expression.
  192.     Each alternative of an "or", except the last, ends with a forward-jump
  193.     of this sort. */
  194.  
  195.   char *fixup_jump = 0;
  196.  
  197.   /* address of start of the most recently finished expression.
  198.     This tells postfix * where to find the start of its operand. */
  199.  
  200.   char *laststart = 0;
  201.  
  202.   /* In processing a repeat, 1 means zero matches is allowed */
  203.  
  204.   char zero_times_ok;
  205.  
  206.   /* In processing a repeat, 1 means many matches is allowed */
  207.  
  208.   char many_times_ok;
  209.  
  210.   /* address of beginning of regexp, or inside of last \( */
  211.  
  212.   char *begalt = b;
  213.  
  214.   /* Stack of information saved by \( and restored by \).
  215.      Four stack elements are pushed by each \(:
  216.        First, the value of b.
  217.        Second, the value of fixup_jump.
  218.        Third, the value of regnum.
  219.        Fourth, the value of begalt.  */
  220.  
  221.   int stackb[40];
  222.   int *stackp = stackb;
  223.   int *stacke = stackb + 40;
  224.   int *stackt;
  225.  
  226.   /* Counts \('s as they are encountered.  Remembered for the matching \),
  227.      where it becomes the "register number" to put in the stop_memory command */
  228.  
  229.   int regnum = 1;
  230.  
  231.   bufp->fastmap_accurate = 0;
  232.  
  233. #ifndef emacs
  234. #ifndef SYNTAX_TABLE
  235.   /*
  236.    * Initialize the syntax table.
  237.    */
  238.    init_syntax_once();
  239. #endif
  240. #endif
  241.  
  242.   if (bufp->allocated == 0)
  243.     {
  244.       bufp->allocated = 28;
  245.       if (bufp->buffer)
  246.     /* EXTEND_BUFFER loses when bufp->allocated is 0 */
  247.     bufp->buffer = (char *) realloc (bufp->buffer, 28);
  248.       else
  249.     /* Caller did not allocate a buffer.  Do it for him */
  250.     bufp->buffer = (char *) malloc (28);
  251.       if (!bufp->buffer) goto memory_exhausted;
  252.       begalt = b = bufp->buffer;
  253.     }
  254.  
  255.   while (p != pend)
  256.     {
  257.       if (b - bufp->buffer > bufp->allocated - 10)
  258.     /* Note that EXTEND_BUFFER clobbers c */
  259.     EXTEND_BUFFER;
  260.  
  261.       PATFETCH (c);
  262.  
  263.       switch (c)
  264.     {
  265.     case '$':
  266.       if (obscure_syntax & RE_TIGHT_VBAR)
  267.         {
  268.           if (! (obscure_syntax & RE_CONTEXT_INDEP_OPS) && p != pend)
  269.         goto normal_char;
  270.           /* Make operand of last vbar end before this `$'.  */
  271.           if (fixup_jump)
  272.         store_jump (fixup_jump, jump, b);
  273.           fixup_jump = 0;
  274.           PATPUSH (endline);
  275.           break;
  276.         }
  277.  
  278.       /* $ means succeed if at end of line, but only in special contexts.
  279.         If randomly in the middle of a pattern, it is a normal character. */
  280.       if (p == pend || *p == '\n'
  281.           || (obscure_syntax & RE_CONTEXT_INDEP_OPS)
  282.           || (obscure_syntax & RE_NO_BK_PARENS
  283.           ? *p == ')'
  284.           : *p == '\\' && p[1] == ')')
  285.           || (obscure_syntax & RE_NO_BK_VBAR
  286.           ? *p == '|'
  287.           : *p == '\\' && p[1] == '|'))
  288.         {
  289.           PATPUSH (endline);
  290.           break;
  291.         }
  292.       goto normal_char;
  293.  
  294.     case '^':
  295.       /* ^ means succeed if at beg of line, but only if no preceding pattern. */
  296.  
  297.       if (laststart && p[-2] != '\n'
  298.           && ! (obscure_syntax & RE_CONTEXT_INDEP_OPS))
  299.         goto normal_char;
  300.       if (obscure_syntax & RE_TIGHT_VBAR)
  301.         {
  302.           if (p != pattern + 1
  303.           && ! (obscure_syntax & RE_CONTEXT_INDEP_OPS))
  304.         goto normal_char;
  305.           PATPUSH (begline);
  306.           begalt = b;
  307.         }
  308.       else
  309.         PATPUSH (begline);
  310.       break;
  311.  
  312.     case '+':
  313.     case '?':
  314.       if (obscure_syntax & RE_BK_PLUS_QM)
  315.         goto normal_char;
  316.     handle_plus:
  317.     case '*':
  318.       /* If there is no previous pattern, char not special. */
  319.       if (!laststart && ! (obscure_syntax & RE_CONTEXT_INDEP_OPS))
  320.         goto normal_char;
  321.       /* If there is a sequence of repetition chars,
  322.          collapse it down to equivalent to just one.  */
  323.       zero_times_ok = 0;
  324.       many_times_ok = 0;
  325.       while (1)
  326.         {
  327.           zero_times_ok |= c != '+';
  328.           many_times_ok |= c != '?';
  329.           if (p == pend)
  330.         break;
  331.           PATFETCH (c);
  332.           if (c == '*')
  333.         ;
  334.           else if (!(obscure_syntax & RE_BK_PLUS_QM)
  335.                && (c == '+' || c == '?'))
  336.         ;
  337.           else if ((obscure_syntax & RE_BK_PLUS_QM)
  338.                && c == '\\')
  339.         {
  340.           int c1;
  341.           PATFETCH (c1);
  342.           if (!(c1 == '+' || c1 == '?'))
  343.             {
  344.               PATUNFETCH;
  345.               PATUNFETCH;
  346.               break;
  347.             }
  348.           c = c1;
  349.         }
  350.           else
  351.         {
  352.           PATUNFETCH;
  353.           break;
  354.         }
  355.         }
  356.  
  357.       /* Star, etc. applied to an empty pattern is equivalent
  358.          to an empty pattern.  */
  359.       if (!laststart)
  360.         break;
  361.  
  362.       /* Now we know whether 0 matches is allowed,
  363.          and whether 2 or more matches is allowed.  */
  364.       if (many_times_ok)
  365.         {
  366.           /* If more than one repetition is allowed,
  367.          put in a backward jump at the end.  */
  368.           store_jump (b, maybe_finalize_jump, laststart - 3);
  369.           b += 3;
  370.         }
  371.       insert_jump (on_failure_jump, laststart, b + 3, b);
  372.       pending_exact = 0;
  373.       b += 3;
  374.       if (!zero_times_ok)
  375.         {
  376.           /* At least one repetition required: insert before the loop
  377.          a skip over the initial on-failure-jump instruction */
  378.           insert_jump (dummy_failure_jump, laststart, laststart + 6, b);
  379.           b += 3;
  380.         }
  381.       break;
  382.  
  383.     case '.':
  384.       laststart = b;
  385.       PATPUSH (anychar);
  386.       break;
  387.  
  388.     case '[':
  389.       while (b - bufp->buffer
  390.          > bufp->allocated - 3 - (1 << BYTEWIDTH) / BYTEWIDTH)
  391.         /* Note that EXTEND_BUFFER clobbers c */
  392.         EXTEND_BUFFER;
  393.  
  394.       laststart = b;
  395.       if (*p == '^')
  396.         PATPUSH (charset_not), p++;
  397.       else
  398.         PATPUSH (charset);
  399.       p1 = p;
  400.  
  401.       PATPUSH ((1 << BYTEWIDTH) / BYTEWIDTH);
  402.       /* Clear the whole map */
  403.       bzero (b, (1 << BYTEWIDTH) / BYTEWIDTH);
  404.       /* Read in characters and ranges, setting map bits */
  405.       while (1)
  406.         {
  407.           PATFETCH (c);
  408.           if (c == ']' && p != p1 + 1) break;
  409.           if (*p == '-' && p[1] != ']')
  410.         {
  411.           PATFETCH (c1);
  412.           PATFETCH (c1);
  413.           while (c <= c1)
  414.             b[c / BYTEWIDTH] |= 1 << (c % BYTEWIDTH), c++;
  415.         }
  416.           else
  417.         {
  418.           b[c / BYTEWIDTH] |= 1 << (c % BYTEWIDTH);
  419.         }
  420.         }
  421.       /* Discard any bitmap bytes that are all 0 at the end of the map.
  422.          Decrement the map-length byte too. */
  423.       while ((int) b[-1] > 0 && b[b[-1] - 1] == 0)
  424.         b[-1]--;
  425.       b += b[-1];
  426.       break;
  427.  
  428.     case '(':
  429.       if (! (obscure_syntax & RE_NO_BK_PARENS))
  430.         goto normal_char;
  431.       else
  432.         goto handle_open;
  433.  
  434.     case ')':
  435.       if (! (obscure_syntax & RE_NO_BK_PARENS))
  436.         goto normal_char;
  437.       else
  438.         goto handle_close;
  439.  
  440.     case '\n':
  441.       if (! (obscure_syntax & RE_NEWLINE_OR))
  442.         goto normal_char;
  443.       else
  444.         goto handle_bar;
  445.  
  446.     case '|':
  447.       if (! (obscure_syntax & RE_NO_BK_VBAR))
  448.         goto normal_char;
  449.       else
  450.         goto handle_bar;
  451.  
  452.         case '\\':
  453.       if (p == pend) goto invalid_pattern;
  454.       PATFETCH_RAW (c);
  455.       switch (c)
  456.         {
  457.         case '(':
  458.           if (obscure_syntax & RE_NO_BK_PARENS)
  459.         goto normal_backsl;
  460.         handle_open:
  461.           if (stackp == stacke) goto nesting_too_deep;
  462.           if (regnum < RE_NREGS)
  463.             {
  464.           PATPUSH (start_memory);
  465.           PATPUSH (regnum);
  466.             }
  467.           *stackp++ = b - bufp->buffer;
  468.           *stackp++ = fixup_jump ? fixup_jump - bufp->buffer + 1 : 0;
  469.           *stackp++ = regnum++;
  470.           *stackp++ = begalt - bufp->buffer;
  471.           fixup_jump = 0;
  472.           laststart = 0;
  473.           begalt = b;
  474.           break;
  475.  
  476.         case ')':
  477.           if (obscure_syntax & RE_NO_BK_PARENS)
  478.         goto normal_backsl;
  479.         handle_close:
  480.           if (stackp == stackb) goto unmatched_close;
  481.           begalt = *--stackp + bufp->buffer;
  482.           if (fixup_jump)
  483.         store_jump (fixup_jump, jump, b);
  484.           if (stackp[-1] < RE_NREGS)
  485.         {
  486.           PATPUSH (stop_memory);
  487.           PATPUSH (stackp[-1]);
  488.         }
  489.           stackp -= 2;
  490.           fixup_jump = 0;
  491.           if (*stackp)
  492.         fixup_jump = *stackp + bufp->buffer - 1;
  493.           laststart = *--stackp + bufp->buffer;
  494.           break;
  495.  
  496.         case '|':
  497.           if (obscure_syntax & RE_NO_BK_VBAR)
  498.         goto normal_backsl;
  499.         handle_bar:
  500.           insert_jump (on_failure_jump, begalt, b + 6, b);
  501.           pending_exact = 0;
  502.           b += 3;
  503.           if (fixup_jump)
  504.         store_jump (fixup_jump, jump, b);
  505.           fixup_jump = b;
  506.           b += 3;
  507.           laststart = 0;
  508.           begalt = b;
  509.           break;
  510.  
  511. #ifdef emacs
  512.         case '=':
  513.           PATPUSH (at_dot);
  514.           break;
  515.  
  516.         case 's':    
  517.           laststart = b;
  518.           PATPUSH (syntaxspec);
  519.           PATFETCH (c);
  520.           PATPUSH (syntax_spec_code[c]);
  521.           break;
  522.  
  523.         case 'S':
  524.           laststart = b;
  525.           PATPUSH (notsyntaxspec);
  526.           PATFETCH (c);
  527.           PATPUSH (syntax_spec_code[c]);
  528.           break;
  529. #endif emacs
  530.  
  531.         case 'w':
  532.           laststart = b;
  533.           PATPUSH (wordchar);
  534.           break;
  535.  
  536.         case 'W':
  537.           laststart = b;
  538.           PATPUSH (notwordchar);
  539.           break;
  540.  
  541.         case '<':
  542.           PATPUSH (wordbeg);
  543.           break;
  544.  
  545.         case '>':
  546.           PATPUSH (wordend);
  547.           break;
  548.  
  549.         case 'b':
  550.           PATPUSH (wordbound);
  551.           break;
  552.  
  553.         case 'B':
  554.           PATPUSH (notwordbound);
  555.           break;
  556.  
  557.         case '`':
  558.           PATPUSH (begbuf);
  559.           break;
  560.  
  561.         case '\'':
  562.           PATPUSH (endbuf);
  563.           break;
  564.  
  565.         case '1':
  566.         case '2':
  567.         case '3':
  568.         case '4':
  569.         case '5':
  570.         case '6':
  571.         case '7':
  572.         case '8':
  573.         case '9':
  574.           c1 = c - '0';
  575.           if (c1 >= regnum)
  576.         goto normal_char;
  577.           for (stackt = stackp - 2;  stackt > stackb;  stackt -= 4)
  578.          if (*stackt == c1)
  579.           goto normal_char;
  580.           laststart = b;
  581.           PATPUSH (duplicate);
  582.           PATPUSH (c1);
  583.           break;
  584.  
  585.         case '+':
  586.         case '?':
  587.           if (obscure_syntax & RE_BK_PLUS_QM)
  588.         goto handle_plus;
  589.  
  590.         default:
  591.         normal_backsl:
  592.           /* You might think it would be useful for \ to mean
  593.          not to translate; but if we don't translate it
  594.          it will never match anything.  */
  595.           if (translate) c = translate[c];
  596.           goto normal_char;
  597.         }
  598.       break;
  599.  
  600.     default:
  601.     normal_char:
  602.       if (!pending_exact || pending_exact + *pending_exact + 1 != b
  603.           || *pending_exact == 0177 || *p == '*' || *p == '^'
  604.           || ((obscure_syntax & RE_BK_PLUS_QM)
  605.           ? *p == '\\' && (p[1] == '+' || p[1] == '?')
  606.           : (*p == '+' || *p == '?')))
  607.         {
  608.           laststart = b;
  609.           PATPUSH (exactn);
  610.           pending_exact = b;
  611.           PATPUSH (0);
  612.         }
  613.       PATPUSH (c);
  614.       (*pending_exact)++;
  615.     }
  616.     }
  617.  
  618.   if (fixup_jump)
  619.     store_jump (fixup_jump, jump, b);
  620.  
  621.   if (stackp != stackb) goto unmatched_open;
  622.  
  623.   bufp->used = b - bufp->buffer;
  624.   return 0;
  625.  
  626.  invalid_pattern:
  627.   return "Invalid regular expression";
  628.  
  629.  unmatched_open:
  630.   return "Unmatched \\(";
  631.  
  632.  unmatched_close:
  633.   return "Unmatched \\)";
  634.  
  635.  end_of_pattern:
  636.   return "Premature end of regular expression";
  637.  
  638.  nesting_too_deep:
  639.   return "Nesting too deep";
  640.  
  641.  too_big:
  642.   return "Regular expression too big";
  643.  
  644.  memory_exhausted:
  645.   return "Memory exhausted";
  646. }
  647.  
  648. /* Store where `from' points a jump operation to jump to where `to' points.
  649.   `opcode' is the opcode to store. */
  650.  
  651. static int
  652. store_jump (from, opcode, to)
  653.      char *from, *to;
  654.      char opcode;
  655. {
  656.   from[0] = opcode;
  657.   from[1] = (to - (from + 3)) & 0377;
  658.   from[2] = (to - (from + 3)) >> 8;
  659. }
  660.  
  661. /* Open up space at char FROM, and insert there a jump to TO.
  662.    CURRENT_END gives te end of the storage no in use,
  663.    so we know how much data to copy up.
  664.    OP is the opcode of the jump to insert.
  665.  
  666.    If you call this function, you must zero out pending_exact.  */
  667.  
  668. static int
  669. insert_jump (op, from, to, current_end)
  670.      char op;
  671.      char *from, *to, *current_end;
  672. {
  673.   register char *pto = current_end + 3;
  674.   register char *pfrom = current_end;
  675.   while (pfrom != from)
  676.     *--pto = *--pfrom;
  677.   store_jump (from, op, to);
  678. }
  679.  
  680. /* Given a pattern, compute a fastmap from it.
  681.  The fastmap records which of the (1 << BYTEWIDTH) possible characters
  682.  can start a string that matches the pattern.
  683.  This fastmap is used by re_search to skip quickly over totally implausible text.
  684.  
  685.  The caller must supply the address of a (1 << BYTEWIDTH)-byte data area
  686.  as bufp->fastmap.
  687.  The other components of bufp describe the pattern to be used.  */
  688.  
  689. void
  690. re_compile_fastmap (bufp)
  691.      struct re_pattern_buffer *bufp;
  692. {
  693.   unsigned char *pattern = (unsigned char *) bufp->buffer;
  694.   int size = bufp->used;
  695.   register char *fastmap = bufp->fastmap;
  696.   register unsigned char *p = pattern;
  697.   register unsigned char *pend = pattern + size;
  698.   register int j, k;
  699.   unsigned char *translate = (unsigned char *) bufp->translate;
  700.  
  701.   unsigned char *stackb[NFAILURES];
  702.   unsigned char **stackp = stackb;
  703.  
  704.   bzero (fastmap, (1 << BYTEWIDTH));
  705.   bufp->fastmap_accurate = 1;
  706.   bufp->can_be_null = 0;
  707.       
  708.   while (p)
  709.     {
  710.       if (p == pend)
  711.     {
  712.       bufp->can_be_null = 1;
  713.       break;
  714.     }
  715. #ifdef SWITCH_ENUM_BUG
  716.       switch ((int) ((enum regexpcode) *p++))
  717. #else
  718.       switch ((enum regexpcode) *p++)
  719. #endif
  720.     {
  721.     case exactn:
  722.       if (translate)
  723.         fastmap[translate[p[1]]] = 1;
  724.       else
  725.         fastmap[p[1]] = 1;
  726.       break;
  727.  
  728.         case begline:
  729.         case before_dot:
  730.     case at_dot:
  731.     case after_dot:
  732.     case begbuf:
  733.     case endbuf:
  734.     case wordbound:
  735.     case notwordbound:
  736.     case wordbeg:
  737.     case wordend:
  738.       continue;
  739.  
  740.     case endline:
  741.       if (translate)
  742.         fastmap[translate['\n']] = 1;
  743.       else
  744.         fastmap['\n'] = 1;
  745.       if (bufp->can_be_null != 1)
  746.         bufp->can_be_null = 2;
  747.       break;
  748.  
  749.     case finalize_jump:
  750.     case maybe_finalize_jump:
  751.     case jump:
  752.     case dummy_failure_jump:
  753.       bufp->can_be_null = 1;
  754.       j = *p++ & 0377;
  755.       j += SIGN_EXTEND_CHAR (*(char *)p) << 8;
  756.       p += j + 1;        /* The 1 compensates for missing ++ above */
  757.       if (j > 0)
  758.         continue;
  759.       /* Jump backward reached implies we just went through
  760.          the body of a loop and matched nothing.
  761.          Opcode jumped to should be an on_failure_jump.
  762.          Just treat it like an ordinary jump.
  763.          For a * loop, it has pushed its failure point already;
  764.          if so, discard that as redundant.  */
  765.       if ((enum regexpcode) *p != on_failure_jump)
  766.         continue;
  767.       p++;
  768.       j = *p++ & 0377;
  769.       j += SIGN_EXTEND_CHAR (*(char *)p) << 8;
  770.       p += j + 1;        /* The 1 compensates for missing ++ above */
  771.       if (stackp != stackb && *stackp == p)
  772.         stackp--;
  773.       continue;
  774.       
  775.     case on_failure_jump:
  776.       j = *p++ & 0377;
  777.       j += SIGN_EXTEND_CHAR (*(char *)p) << 8;
  778.       p++;
  779.       *++stackp = p + j;
  780.       continue;
  781.  
  782.     case start_memory:
  783.     case stop_memory:
  784.       p++;
  785.       continue;
  786.  
  787.     case duplicate:
  788.       bufp->can_be_null = 1;
  789.       fastmap['\n'] = 1;
  790.     case anychar:
  791.       for (j = 0; j < (1 << BYTEWIDTH); j++)
  792.         if (j != '\n')
  793.           fastmap[j] = 1;
  794.       if (bufp->can_be_null)
  795.         return;
  796.       /* Don't return; check the alternative paths
  797.          so we can set can_be_null if appropriate.  */
  798.       break;
  799.  
  800.     case wordchar:
  801.       for (j = 0; j < (1 << BYTEWIDTH); j++)
  802.         if (SYNTAX (j) == Sword)
  803.           fastmap[j] = 1;
  804.       break;
  805.  
  806.     case notwordchar:
  807.       for (j = 0; j < (1 << BYTEWIDTH); j++)
  808.         if (SYNTAX (j) != Sword)
  809.           fastmap[j] = 1;
  810.       break;
  811.  
  812. #ifdef emacs
  813.     case syntaxspec:
  814.       k = *p++;
  815.       for (j = 0; j < (1 << BYTEWIDTH); j++)
  816.         if (SYNTAX (j) == (enum syntaxcode) k)
  817.           fastmap[j] = 1;
  818.       break;
  819.  
  820.     case notsyntaxspec:
  821.       k = *p++;
  822.       for (j = 0; j < (1 << BYTEWIDTH); j++)
  823.         if (SYNTAX (j) != (enum syntaxcode) k)
  824.           fastmap[j] = 1;
  825.       break;
  826. #endif emacs
  827.  
  828.     case charset:
  829.       for (j = *p++ * BYTEWIDTH - 1; j >= 0; j--)
  830.         if (p[j / BYTEWIDTH] & (1 << (j % BYTEWIDTH)))
  831.           {
  832.         if (translate)
  833.           fastmap[translate[j]] = 1;
  834.         else
  835.           fastmap[j] = 1;
  836.           }
  837.       break;
  838.  
  839.     case charset_not:
  840.       /* Chars beyond end of map must be allowed */
  841.       for (j = *p * BYTEWIDTH; j < (1 << BYTEWIDTH); j++)
  842.         if (translate)
  843.           fastmap[translate[j]] = 1;
  844.         else
  845.           fastmap[j] = 1;
  846.  
  847.       for (j = *p++ * BYTEWIDTH - 1; j >= 0; j--)
  848.         if (!(p[j / BYTEWIDTH] & (1 << (j % BYTEWIDTH))))
  849.           {
  850.         if (translate)
  851.           fastmap[translate[j]] = 1;
  852.         else
  853.           fastmap[j] = 1;
  854.           }
  855.       break;
  856.     }
  857.  
  858.       /* Get here means we have successfully found the possible starting characters
  859.      of one path of the pattern.  We need not follow this path any farther.
  860.      Instead, look at the next alternative remembered in the stack. */
  861.       if (stackp != stackb)
  862.     p = *stackp--;
  863.       else
  864.     break;
  865.     }
  866. }
  867.  
  868. /* Like re_search_2, below, but only one string is specified. */
  869.  
  870. int
  871. re_search (pbufp, string, size, startpos, range, regs)
  872.      struct re_pattern_buffer *pbufp;
  873.      char *string;
  874.      int size, startpos, range;
  875.      struct re_registers *regs;
  876. {
  877.   return re_search_2 (pbufp, 0, 0, string, size, startpos, range, regs, size);
  878. }
  879.  
  880. /* Like re_match_2 but tries first a match starting at index STARTPOS,
  881.    then at STARTPOS + 1, and so on.
  882.    RANGE is the number of places to try before giving up.
  883.    If RANGE is negative, the starting positions tried are
  884.     STARTPOS, STARTPOS - 1, etc.
  885.    It is up to the caller to make sure that range is not so large
  886.    as to take the starting position outside of the input strings.
  887.  
  888. The value returned is the position at which the match was found,
  889.  or -1 if no match was found,
  890.  or -2 if error (such as failure stack overflow).  */
  891.  
  892. int
  893. re_search_2 (pbufp, string1, size1, string2, size2, startpos, range, regs, mstop)
  894.      struct re_pattern_buffer *pbufp;
  895.      char *string1, *string2;
  896.      int size1, size2;
  897.      int startpos;
  898.      register int range;
  899.      struct re_registers *regs;
  900.      int mstop;
  901. {
  902.   register char *fastmap = pbufp->fastmap;
  903.   register unsigned char *translate = (unsigned char *) pbufp->translate;
  904.   int total = size1 + size2;
  905.   int val;
  906.  
  907.   /* Update the fastmap now if not correct already */
  908.   if (fastmap && !pbufp->fastmap_accurate)
  909.     re_compile_fastmap (pbufp);
  910.   
  911.   /* Don't waste time in a long search for a pattern
  912.      that says it is anchored.  */
  913.   if (pbufp->used > 0 && (enum regexpcode) pbufp->buffer[0] == begbuf
  914.       && range > 0)
  915.     {
  916.       if (startpos > 0)
  917.     return -1;
  918.       else
  919.     range = 1;
  920.     }
  921.  
  922.   while (1)
  923.     {
  924.       /* If a fastmap is supplied, skip quickly over characters
  925.      that cannot possibly be the start of a match.
  926.      Note, however, that if the pattern can possibly match
  927.      the null string, we must test it at each starting point
  928.      so that we take the first null string we get.  */
  929.  
  930.       if (fastmap && startpos < total && pbufp->can_be_null != 1)
  931.     {
  932.       if (range > 0)
  933.         {
  934.           register int lim = 0;
  935.           register unsigned char *p;
  936.           int irange = range;
  937.           if (startpos < size1 && startpos + range >= size1)
  938.         lim = range - (size1 - startpos);
  939.  
  940.           p = ((unsigned char *)
  941.            &(startpos >= size1 ? string2 - size1 : string1)[startpos]);
  942.  
  943.           if (translate)
  944.         {
  945.           while (range > lim && !fastmap[translate[*p++]])
  946.             range--;
  947.         }
  948.           else
  949.         {
  950.           while (range > lim && !fastmap[*p++])
  951.             range--;
  952.         }
  953.           startpos += irange - range;
  954.         }
  955.       else
  956.         {
  957.           register unsigned char c;
  958.           if (startpos >= size1)
  959.         c = string2[startpos - size1];
  960.           else
  961.         c = string1[startpos];
  962.           c &= 0xff;
  963.           if (translate ? !fastmap[translate[c]] : !fastmap[c])
  964.         goto advance;
  965.         }
  966.     }
  967.  
  968.       if (range >= 0 && startpos == total
  969.       && fastmap && pbufp->can_be_null == 0)
  970.     return -1;
  971.  
  972.       val = re_match_2 (pbufp, string1, size1, string2, size2, startpos, regs,
  973.             mstop);
  974.       /* Propagate error indication if worse than mere failure.  */
  975.       if (val == -2)
  976.     return -2;
  977.       /* Return position on success.  */
  978.       if (0 <= val)
  979.     return startpos;
  980.  
  981. #ifdef C_ALLOCA
  982.       alloca (0);
  983. #endif /* C_ALLOCA */
  984.  
  985.     advance:
  986.       if (!range) break;
  987.       if (range > 0) range--, startpos++; else range++, startpos--;
  988.     }
  989.   return -1;
  990. }
  991.  
  992. #ifndef emacs   /* emacs never uses this */
  993. int
  994. re_match (pbufp, string, size, pos, regs)
  995.      struct re_pattern_buffer *pbufp;
  996.      char *string;
  997.      int size, pos;
  998.      struct re_registers *regs;
  999. {
  1000.   return re_match_2 (pbufp, 0, 0, string, size, pos, regs, size);
  1001. }
  1002. #endif /* emacs */
  1003.  
  1004. /* Maximum size of failure stack.  Beyond this, overflow is an error.  */
  1005.  
  1006. int re_max_failures = 2000;
  1007.  
  1008. static int bcmp_translate();
  1009. /* Match the pattern described by PBUFP
  1010.    against data which is the virtual concatenation of STRING1 and STRING2.
  1011.    SIZE1 and SIZE2 are the sizes of the two data strings.
  1012.    Start the match at position POS.
  1013.    Do not consider matching past the position MSTOP.
  1014.  
  1015.    If pbufp->fastmap is nonzero, then it had better be up to date.
  1016.  
  1017.    The reason that the data to match are specified as two components
  1018.    which are to be regarded as concatenated
  1019.    is so this function can be used directly on the contents of an Emacs buffer.
  1020.  
  1021.    -1 is returned if there is no match.  -2 is returned if there is
  1022.    an error (such as match stack overflow).  Otherwise the value is the length
  1023.    of the substring which was matched.  */
  1024.  
  1025. int
  1026. re_match_2 (pbufp, string1, size1, string2, size2, pos, regs, mstop)
  1027.      struct re_pattern_buffer *pbufp;
  1028.      unsigned char *string1, *string2;
  1029.      int size1, size2;
  1030.      int pos;
  1031.      struct re_registers *regs;
  1032.      int mstop;
  1033. {
  1034.   register unsigned char *p = (unsigned char *) pbufp->buffer;
  1035.   register unsigned char *pend = p + pbufp->used;
  1036.   /* End of first string */
  1037.   unsigned char *end1;
  1038.   /* End of second string */
  1039.   unsigned char *end2;
  1040.   /* Pointer just past last char to consider matching */
  1041.   unsigned char *end_match_1, *end_match_2;
  1042.   register unsigned char *d, *dend;
  1043.   register int mcnt;
  1044.   unsigned char *translate = (unsigned char *) pbufp->translate;
  1045.  
  1046.  /* Failure point stack.  Each place that can handle a failure further down the line
  1047.     pushes a failure point on this stack.  It consists of two char *'s.
  1048.     The first one pushed is where to resume scanning the pattern;
  1049.     the second pushed is where to resume scanning the strings.
  1050.     If the latter is zero, the failure point is a "dummy".
  1051.     If a failure happens and the innermost failure point is dormant,
  1052.     it discards that failure point and tries the next one. */
  1053.  
  1054.   unsigned char *initial_stack[2 * NFAILURES];
  1055.   unsigned char **stackb = initial_stack;
  1056.   unsigned char **stackp = stackb, **stacke = &stackb[2 * NFAILURES];
  1057.  
  1058.   /* Information on the "contents" of registers.
  1059.      These are pointers into the input strings; they record
  1060.      just what was matched (on this attempt) by some part of the pattern.
  1061.      The start_memory command stores the start of a register's contents
  1062.      and the stop_memory command stores the end.
  1063.  
  1064.      At that point, regstart[regnum] points to the first character in the register,
  1065.      regend[regnum] points to the first character beyond the end of the register,
  1066.      regstart_seg1[regnum] is true iff regstart[regnum] points into string1,
  1067.      and regend_seg1[regnum] is true iff regend[regnum] points into string1.  */
  1068.  
  1069.   unsigned char *regstart[RE_NREGS];
  1070.   unsigned char *regend[RE_NREGS];
  1071.   unsigned char regstart_seg1[RE_NREGS], regend_seg1[RE_NREGS];
  1072.  
  1073.   /* Set up pointers to ends of strings.
  1074.      Don't allow the second string to be empty unless both are empty.  */
  1075.   if (!size2)
  1076.     {
  1077.       string2 = string1;
  1078.       size2 = size1;
  1079.       string1 = 0;
  1080.       size1 = 0;
  1081.     }
  1082.   end1 = string1 + size1;
  1083.   end2 = string2 + size2;
  1084.  
  1085.   /* Compute where to stop matching, within the two strings */
  1086.   if (mstop <= size1)
  1087.     {
  1088.       end_match_1 = string1 + mstop;
  1089.       end_match_2 = string2;
  1090.     }
  1091.   else
  1092.     {
  1093.       end_match_1 = end1;
  1094.       end_match_2 = string2 + mstop - size1;
  1095.     }
  1096.  
  1097.   /* Initialize \) text positions to -1
  1098.      to mark ones that no \( or \) has been seen for.  */
  1099.  
  1100.   for (mcnt = 0; mcnt < sizeof (regend) / sizeof (*regend); mcnt++)
  1101.     regend[mcnt] = (unsigned char *) -1;
  1102.  
  1103.   /* `p' scans through the pattern as `d' scans through the data.
  1104.      `dend' is the end of the input string that `d' points within.
  1105.      `d' is advanced into the following input string whenever necessary,
  1106.      but this happens before fetching;
  1107.      therefore, at the beginning of the loop,
  1108.      `d' can be pointing at the end of a string,
  1109.      but it cannot equal string2.  */
  1110.  
  1111.   if (pos <= size1)
  1112.     d = string1 + pos, dend = end_match_1;
  1113.   else
  1114.     d = string2 + pos - size1, dend = end_match_2;
  1115.  
  1116. /* Write PREFETCH; just before fetching a character with *d.  */
  1117. #define PREFETCH \
  1118.  while (d == dend)                            \
  1119.   { if (dend == end_match_2) goto fail;  /* end of string2 => failure */   \
  1120.     d = string2;  /* end of string1 => advance to string2. */       \
  1121.     dend = end_match_2; }
  1122.  
  1123.   /* This loop loops over pattern commands.
  1124.      It exits by returning from the function if match is complete,
  1125.      or it drops through if match fails at this starting point in the input data. */
  1126.  
  1127.   while (1)
  1128.     {
  1129.       if (p == pend)
  1130.     /* End of pattern means we have succeeded! */
  1131.     {
  1132.       /* If caller wants register contents data back, convert it to indices */
  1133.       if (regs)
  1134.         {
  1135.            regs->start[0] = pos;
  1136.            if (dend == end_match_1)
  1137.          regs->end[0] = d - string1;
  1138.            else
  1139.          regs->end[0] = d - string2 + size1;
  1140.            for (mcnt = 1; mcnt < RE_NREGS; mcnt++)
  1141.         {
  1142.           if (regend[mcnt] == (unsigned char *) -1)
  1143.             {
  1144.               regs->start[mcnt] = -1;
  1145.               regs->end[mcnt] = -1;
  1146.               continue;
  1147.             }
  1148.            if (regstart_seg1[mcnt])
  1149.             regs->start[mcnt] = regstart[mcnt] - string1;
  1150.           else
  1151.             regs->start[mcnt] = regstart[mcnt] - string2 + size1;
  1152.            if (regend_seg1[mcnt])
  1153.             regs->end[mcnt] = regend[mcnt] - string1;
  1154.           else
  1155.             regs->end[mcnt] = regend[mcnt] - string2 + size1;
  1156.         }
  1157.         }
  1158.        if (dend == end_match_1)
  1159.         return (d - string1 - pos);
  1160.       else
  1161.         return d - string2 + size1 - pos;
  1162.     }
  1163.  
  1164.       /* Otherwise match next pattern command */
  1165. #ifdef SWITCH_ENUM_BUG
  1166.       switch ((int) ((enum regexpcode) *p++))
  1167. #else
  1168.       switch ((enum regexpcode) *p++)
  1169. #endif
  1170.     {
  1171.  
  1172.     /* \( is represented by a start_memory, \) by a stop_memory.
  1173.         Both of those commands contain a "register number" argument.
  1174.         The text matched within the \( and \) is recorded under that number.
  1175.         Then, \<digit> turns into a `duplicate' command which
  1176.         is followed by the numeric value of <digit> as the register number. */
  1177.  
  1178.     case start_memory:
  1179.       regstart[*p] = d;
  1180.        regstart_seg1[*p++] = (dend == end_match_1);
  1181.       break;
  1182.  
  1183.     case stop_memory:
  1184.       regend[*p] = d;
  1185.        regend_seg1[*p++] = (dend == end_match_1);
  1186.       break;
  1187.  
  1188.     case duplicate:
  1189.       {
  1190.         int regno = *p++;   /* Get which register to match against */
  1191.         register unsigned char *d2, *dend2;
  1192.  
  1193.         /* Don't allow matching a register that hasn't been used.
  1194.            This isn't fully reliable in the current version,
  1195.            but it is better than crashing.  */
  1196.         if ((int) regend[regno] <= -1)
  1197.           goto fail;
  1198.  
  1199.         d2 = regstart[regno];
  1200.          dend2 = ((regstart_seg1[regno] == regend_seg1[regno])
  1201.              ? regend[regno] : end_match_1);
  1202.         while (1)
  1203.           {
  1204.         /* Advance to next segment in register contents, if necessary */
  1205.         while (d2 == dend2)
  1206.           {
  1207.             if (dend2 == end_match_2) break;
  1208.             if (dend2 == regend[regno]) break;
  1209.             d2 = string2, dend2 = regend[regno];  /* end of string1 => advance to string2. */
  1210.           }
  1211.         /* At end of register contents => success */
  1212.         if (d2 == dend2) break;
  1213.  
  1214.         /* Advance to next segment in data being matched, if necessary */
  1215.         PREFETCH;
  1216.  
  1217.         /* mcnt gets # consecutive chars to compare */
  1218.         mcnt = dend - d;
  1219.         if (mcnt > dend2 - d2)
  1220.           mcnt = dend2 - d2;
  1221.         /* Compare that many; failure if mismatch, else skip them. */
  1222.         if (translate ? bcmp_translate (d, d2, mcnt, translate) : bcmp (d, d2, mcnt))
  1223.           goto fail;
  1224.         d += mcnt, d2 += mcnt;
  1225.           }
  1226.       }
  1227.       break;
  1228.  
  1229.     case anychar:
  1230.       /* fetch a data character */
  1231.       PREFETCH;
  1232.       /* Match anything but a newline.  */
  1233.       if ((translate ? translate[*d++] : *d++) == '\n')
  1234.         goto fail;
  1235.       break;
  1236.  
  1237.     case charset:
  1238.     case charset_not:
  1239.       {
  1240.         /* Nonzero for charset_not */
  1241.         int not = 0;
  1242.         register int c;
  1243.         if (*(p - 1) == (unsigned char) charset_not)
  1244.           not = 1;
  1245.  
  1246.         /* fetch a data character */
  1247.         PREFETCH;
  1248.  
  1249.         if (translate)
  1250.           c = translate [*d];
  1251.         else
  1252.           c = *d;
  1253.  
  1254.         if (c < *p * BYTEWIDTH
  1255.         && p[1 + c / BYTEWIDTH] & (1 << (c % BYTEWIDTH)))
  1256.           not = !not;
  1257.  
  1258.         p += 1 + *p;
  1259.  
  1260.         if (!not) goto fail;
  1261.         d++;
  1262.         break;
  1263.       }
  1264.  
  1265.     case begline:
  1266.       if (d == string1 || d[-1] == '\n')
  1267.         break;
  1268.       goto fail;
  1269.  
  1270.     case endline:
  1271.       if (d == end2
  1272.           || (d == end1 ? (size2 == 0 || *string2 == '\n') : *d == '\n'))
  1273.         break;
  1274.       goto fail;
  1275.  
  1276.     /* "or" constructs ("|") are handled by starting each alternative
  1277.         with an on_failure_jump that points to the start of the next alternative.
  1278.         Each alternative except the last ends with a jump to the joining point.
  1279.         (Actually, each jump except for the last one really jumps
  1280.          to the following jump, because tensioning the jumps is a hassle.) */
  1281.  
  1282.     /* The start of a stupid repeat has an on_failure_jump that points
  1283.        past the end of the repeat text.
  1284.        This makes a failure point so that, on failure to match a repetition,
  1285.        matching restarts past as many repetitions have been found
  1286.        with no way to fail and look for another one.  */
  1287.  
  1288.     /* A smart repeat is similar but loops back to the on_failure_jump
  1289.        so that each repetition makes another failure point. */
  1290.  
  1291.     case on_failure_jump:
  1292.       if (stackp == stacke)
  1293.         {
  1294.           unsigned char **stackx;
  1295.           if (stacke - stackb > re_max_failures)
  1296.         return -2;
  1297.           stackx = (unsigned char **) alloca (2 * (stacke - stackb)
  1298.                      * sizeof (char *));
  1299.           bcopy (stackb, stackx, (stacke - stackb) * sizeof (char *));
  1300.           stackp = stackx + (stackp - stackb);
  1301.           stacke = stackx + 2 * (stacke - stackb);
  1302.           stackb = stackx;
  1303.         }
  1304.       mcnt = *p++ & 0377;
  1305.       mcnt += SIGN_EXTEND_CHAR (*(char *)p) << 8;
  1306.       p++;
  1307.       *stackp++ = mcnt + p;
  1308.       *stackp++ = d;
  1309.       break;
  1310.  
  1311.     /* The end of a smart repeat has an maybe_finalize_jump back.
  1312.        Change it either to a finalize_jump or an ordinary jump. */
  1313.  
  1314.     case maybe_finalize_jump:
  1315.       mcnt = *p++ & 0377;
  1316.       mcnt += SIGN_EXTEND_CHAR (*(char *)p) << 8;
  1317.       p++;
  1318.       /* Compare what follows with the begining of the repeat.
  1319.          If we can establish that there is nothing that they would
  1320.          both match, we can change to finalize_jump */
  1321.       if (p == pend)
  1322.         p[-3] = (unsigned char) finalize_jump;
  1323.       else if (*p == (unsigned char) exactn
  1324.            || *p == (unsigned char) endline)
  1325.         {
  1326.           register int c = *p == (unsigned char) endline ? '\n' : p[2];
  1327.           register unsigned char *p1 = p + mcnt;
  1328.           /* p1[0] ... p1[2] are an on_failure_jump.
  1329.          Examine what follows that */
  1330.           if (p1[3] == (unsigned char) exactn && p1[5] != c)
  1331.         p[-3] = (unsigned char) finalize_jump;
  1332.           else if (p1[3] == (unsigned char) charset
  1333.                || p1[3] == (unsigned char) charset_not)
  1334.         {
  1335.           int not = p1[3] == (unsigned char) charset_not;
  1336.           if (c < p1[4] * BYTEWIDTH
  1337.               && p1[5 + c / BYTEWIDTH] & (1 << (c % BYTEWIDTH)))
  1338.             not = !not;
  1339.           /* not is 1 if c would match */
  1340.           /* That means it is not safe to finalize */
  1341.           if (!not)
  1342.             p[-3] = (unsigned char) finalize_jump;
  1343.         }
  1344.         }
  1345.       p -= 2;
  1346.       if (p[-1] != (unsigned char) finalize_jump)
  1347.         {
  1348.           p[-1] = (unsigned char) jump;
  1349.           goto nofinalize;
  1350.         }
  1351.  
  1352.     /* The end of a stupid repeat has a finalize-jump
  1353.        back to the start, where another failure point will be made
  1354.        which will point after all the repetitions found so far. */
  1355.  
  1356.     case finalize_jump:
  1357.       stackp -= 2;
  1358.  
  1359.     case jump:
  1360.     nofinalize:
  1361.       mcnt = *p++ & 0377;
  1362.       mcnt += SIGN_EXTEND_CHAR (*(char *)p) << 8;
  1363.       p += mcnt + 1;    /* The 1 compensates for missing ++ above */
  1364.       break;
  1365.  
  1366.     case dummy_failure_jump:
  1367.       if (stackp == stacke)
  1368.         {
  1369.           unsigned char **stackx
  1370.         = (unsigned char **) alloca (2 * (stacke - stackb)
  1371.                          * sizeof (char *));
  1372.           bcopy (stackb, stackx, (stacke - stackb) * sizeof (char *));
  1373.           stackp = stackx + (stackp - stackb);
  1374.           stacke = stackx + 2 * (stacke - stackb);
  1375.           stackb = stackx;
  1376.         }
  1377.       *stackp++ = 0;
  1378.       *stackp++ = 0;
  1379.       goto nofinalize;
  1380.  
  1381.     case wordbound:
  1382.       if (d == string1  /* Points to first char */
  1383.           || d == end2  /* Points to end */
  1384.           || (d == end1 && size2 == 0)) /* Points to end */
  1385.         break;
  1386.       if ((SYNTAX (d[-1]) == Sword)
  1387.           != (SYNTAX (d == end1 ? *string2 : *d) == Sword))
  1388.         break;
  1389.       goto fail;
  1390.  
  1391.     case notwordbound:
  1392.       if (d == string1  /* Points to first char */
  1393.           || d == end2  /* Points to end */
  1394.           || (d == end1 && size2 == 0)) /* Points to end */
  1395.         goto fail;
  1396.       if ((SYNTAX (d[-1]) == Sword)
  1397.           != (SYNTAX (d == end1 ? *string2 : *d) == Sword))
  1398.         goto fail;
  1399.       break;
  1400.  
  1401.     case wordbeg:
  1402.       if (d == end2  /* Points to end */
  1403.           || (d == end1 && size2 == 0) /* Points to end */
  1404.           || SYNTAX (* (d == end1 ? string2 : d)) != Sword) /* Next char not a letter */
  1405.         goto fail;
  1406.       if (d == string1  /* Points to first char */
  1407.           || SYNTAX (d[-1]) != Sword)  /* prev char not letter */
  1408.         break;
  1409.       goto fail;
  1410.  
  1411.     case wordend:
  1412.       if (d == string1  /* Points to first char */
  1413.           || SYNTAX (d[-1]) != Sword)  /* prev char not letter */
  1414.         goto fail;
  1415.       if (d == end2  /* Points to end */
  1416.           || (d == end1 && size2 == 0) /* Points to end */
  1417.           || SYNTAX (d == end1 ? *string2 : *d) != Sword) /* Next char not a letter */
  1418.         break;
  1419.       goto fail;
  1420.  
  1421. #ifdef emacs
  1422.     case before_dot:
  1423.       if (PTR_CHAR_POS (d) + 1 >= point)
  1424.         goto fail;
  1425.       break;
  1426.  
  1427.     case at_dot:
  1428.       if (PTR_CHAR_POS (d) + 1 != point)
  1429.         goto fail;
  1430.       break;
  1431.  
  1432.     case after_dot:
  1433.       if (PTR_CHAR_POS (d) + 1 <= point)
  1434.         goto fail;
  1435.       break;
  1436.  
  1437.     case wordchar:
  1438.       mcnt = (int) Sword;
  1439.       goto matchsyntax;
  1440.  
  1441.     case syntaxspec:
  1442.       mcnt = *p++;
  1443.     matchsyntax:
  1444.       PREFETCH;
  1445.       if (SYNTAX (*d++) != (enum syntaxcode) mcnt) goto fail;
  1446.       break;
  1447.       
  1448.     case notwordchar:
  1449.       mcnt = (int) Sword;
  1450.       goto matchnotsyntax;
  1451.  
  1452.     case notsyntaxspec:
  1453.       mcnt = *p++;
  1454.     matchnotsyntax:
  1455.       PREFETCH;
  1456.       if (SYNTAX (*d++) == (enum syntaxcode) mcnt) goto fail;
  1457.       break;
  1458. #else
  1459.     case wordchar:
  1460.       PREFETCH;
  1461.       if (SYNTAX (*d++) == 0) goto fail;
  1462.       break;
  1463.       
  1464.     case notwordchar:
  1465.       PREFETCH;
  1466.       if (SYNTAX (*d++) != 0) goto fail;
  1467.       break;
  1468. #endif not emacs
  1469.  
  1470.     case begbuf:
  1471.       if (d == string1)    /* Note, d cannot equal string2 */
  1472.         break;        /* unless string1 == string2.  */
  1473.       goto fail;
  1474.  
  1475.     case endbuf:
  1476.       if (d == end2 || (d == end1 && size2 == 0))
  1477.         break;
  1478.       goto fail;
  1479.  
  1480.     case exactn:
  1481.       /* Match the next few pattern characters exactly.
  1482.          mcnt is how many characters to match. */
  1483.       mcnt = *p++;
  1484.       if (translate)
  1485.         {
  1486.           do
  1487.         {
  1488.           PREFETCH;
  1489.           if (translate[*d++] != *p++) goto fail;
  1490.         }
  1491.           while (--mcnt);
  1492.         }
  1493.       else
  1494.         {
  1495.           do
  1496.         {
  1497.           PREFETCH;
  1498.           if (*d++ != *p++) goto fail;
  1499.         }
  1500.           while (--mcnt);
  1501.         }
  1502.       break;
  1503.     }
  1504.       continue;    /* Successfully matched one pattern command; keep matching */
  1505.  
  1506.       /* Jump here if any matching operation fails. */
  1507.     fail:
  1508.       if (stackp != stackb)
  1509.     /* A restart point is known.  Restart there and pop it. */
  1510.     {
  1511.       if (!stackp[-2])
  1512.         {   /* If innermost failure point is dormant, flush it and keep looking */
  1513.           stackp -= 2;
  1514.           goto fail;
  1515.         }
  1516.       d = *--stackp;
  1517.       p = *--stackp;
  1518.       if (d >= string1 && d <= end1)
  1519.         dend = end_match_1;
  1520.     }
  1521.       else break;   /* Matching at this starting point really fails! */
  1522.     }
  1523.   return -1;         /* Failure to match */
  1524. }
  1525.  
  1526. static int
  1527. bcmp_translate (s1, s2, len, translate)
  1528.      unsigned char *s1, *s2;
  1529.      register int len;
  1530.      unsigned char *translate;
  1531. {
  1532.   register unsigned char *p1 = s1, *p2 = s2;
  1533.   while (len)
  1534.     {
  1535.       if (translate [*p1++] != translate [*p2++]) return 1;
  1536.       len--;
  1537.     }
  1538.   return 0;
  1539. }
  1540.  
  1541. /* Entry points compatible with bsd4.2 regex library */
  1542.  
  1543. #ifndef emacs
  1544.  
  1545. static struct re_pattern_buffer re_comp_buf;
  1546.  
  1547. char *
  1548. re_comp (s)
  1549.      char *s;
  1550. {
  1551.   if (!s)
  1552.     {
  1553.       if (!re_comp_buf.buffer)
  1554.     return "No previous regular expression";
  1555.       return 0;
  1556.     }
  1557.  
  1558.   if (!re_comp_buf.buffer)
  1559.     {
  1560.       if (!(re_comp_buf.buffer = (char *) malloc (200)))
  1561.     return "Memory exhausted";
  1562.       re_comp_buf.allocated = 200;
  1563.       if (!(re_comp_buf.fastmap = (char *) malloc (1 << BYTEWIDTH)))
  1564.     return "Memory exhausted";
  1565.     }
  1566.   return re_compile_pattern (s, strlen (s), &re_comp_buf);
  1567. }
  1568.  
  1569. int
  1570. re_exec (s)
  1571.      char *s;
  1572. {
  1573.   int len = strlen (s);
  1574.   return 0 <= re_search (&re_comp_buf, s, len, 0, len, 0);
  1575. }
  1576.  
  1577. #endif /* emacs */
  1578.  
  1579. #ifdef test
  1580.  
  1581. #include <stdio.h>
  1582.  
  1583. /* Indexed by a character, gives the upper case equivalent of the character */
  1584.  
  1585. static char upcase[0400] = 
  1586.   { 000, 001, 002, 003, 004, 005, 006, 007,
  1587.     010, 011, 012, 013, 014, 015, 016, 017,
  1588.     020, 021, 022, 023, 024, 025, 026, 027,
  1589.     030, 031, 032, 033, 034, 035, 036, 037,
  1590.     040, 041, 042, 043, 044, 045, 046, 047,
  1591.     050, 051, 052, 053, 054, 055, 056, 057,
  1592.     060, 061, 062, 063, 064, 065, 066, 067,
  1593.     070, 071, 072, 073, 074, 075, 076, 077,
  1594.     0100, 0101, 0102, 0103, 0104, 0105, 0106, 0107,
  1595.     0110, 0111, 0112, 0113, 0114, 0115, 0116, 0117,
  1596.     0120, 0121, 0122, 0123, 0124, 0125, 0126, 0127,
  1597.     0130, 0131, 0132, 0133, 0134, 0135, 0136, 0137,
  1598.     0140, 0101, 0102, 0103, 0104, 0105, 0106, 0107,
  1599.     0110, 0111, 0112, 0113, 0114, 0115, 0116, 0117,
  1600.     0120, 0121, 0122, 0123, 0124, 0125, 0126, 0127,
  1601.     0130, 0131, 0132, 0173, 0174, 0175, 0176, 0177,
  1602.     0200, 0201, 0202, 0203, 0204, 0205, 0206, 0207,
  1603.     0210, 0211, 0212, 0213, 0214, 0215, 0216, 0217,
  1604.     0220, 0221, 0222, 0223, 0224, 0225, 0226, 0227,
  1605.     0230, 0231, 0232, 0233, 0234, 0235, 0236, 0237,
  1606.     0240, 0241, 0242, 0243, 0244, 0245, 0246, 0247,
  1607.     0250, 0251, 0252, 0253, 0254, 0255, 0256, 0257,
  1608.     0260, 0261, 0262, 0263, 0264, 0265, 0266, 0267,
  1609.     0270, 0271, 0272, 0273, 0274, 0275, 0276, 0277,
  1610.     0300, 0301, 0302, 0303, 0304, 0305, 0306, 0307,
  1611.     0310, 0311, 0312, 0313, 0314, 0315, 0316, 0317,
  1612.     0320, 0321, 0322, 0323, 0324, 0325, 0326, 0327,
  1613.     0330, 0331, 0332, 0333, 0334, 0335, 0336, 0337,
  1614.     0340, 0341, 0342, 0343, 0344, 0345, 0346, 0347,
  1615.     0350, 0351, 0352, 0353, 0354, 0355, 0356, 0357,
  1616.     0360, 0361, 0362, 0363, 0364, 0365, 0366, 0367,
  1617.     0370, 0371, 0372, 0373, 0374, 0375, 0376, 0377
  1618.   };
  1619.  
  1620. main (argc, argv)
  1621.      int argc;
  1622.      char **argv;
  1623. {
  1624.   char pat[80];
  1625.   struct re_pattern_buffer buf;
  1626.   int i;
  1627.   char c;
  1628.   char fastmap[(1 << BYTEWIDTH)];
  1629.  
  1630.   /* Allow a command argument to specify the style of syntax.  */
  1631.   if (argc > 1)
  1632.     obscure_syntax = atoi (argv[1]);
  1633.  
  1634.   buf.allocated = 40;
  1635.   buf.buffer = (char *) malloc (buf.allocated);
  1636.   buf.fastmap = fastmap;
  1637.   buf.translate = upcase;
  1638.  
  1639.   while (1)
  1640.     {
  1641.       gets (pat);
  1642.  
  1643.       if (*pat)
  1644.     {
  1645.           re_compile_pattern (pat, strlen(pat), &buf);
  1646.  
  1647.       for (i = 0; i < buf.used; i++)
  1648.         printchar (buf.buffer[i]);
  1649.  
  1650.       putchar ('\n');
  1651.  
  1652.       printf ("%d allocated, %d used.\n", buf.allocated, buf.used);
  1653.  
  1654.       re_compile_fastmap (&buf);
  1655.       printf ("Allowed by fastmap: ");
  1656.       for (i = 0; i < (1 << BYTEWIDTH); i++)
  1657.         if (fastmap[i]) printchar (i);
  1658.       putchar ('\n');
  1659.     }
  1660.  
  1661.       gets (pat);    /* Now read the string to match against */
  1662.  
  1663.       i = re_match (&buf, pat, strlen (pat), 0, 0);
  1664.       printf ("Match value %d.\n", i);
  1665.     }
  1666. }
  1667.  
  1668. #ifdef NOTDEF
  1669. print_buf (bufp)
  1670.      struct re_pattern_buffer *bufp;
  1671. {
  1672.   int i;
  1673.  
  1674.   printf ("buf is :\n----------------\n");
  1675.   for (i = 0; i < bufp->used; i++)
  1676.     printchar (bufp->buffer[i]);
  1677.   
  1678.   printf ("\n%d allocated, %d used.\n", bufp->allocated, bufp->used);
  1679.   
  1680.   printf ("Allowed by fastmap: ");
  1681.   for (i = 0; i < (1 << BYTEWIDTH); i++)
  1682.     if (bufp->fastmap[i])
  1683.       printchar (i);
  1684.   printf ("\nAllowed by translate: ");
  1685.   if (bufp->translate)
  1686.     for (i = 0; i < (1 << BYTEWIDTH); i++)
  1687.       if (bufp->translate[i])
  1688.     printchar (i);
  1689.   printf ("\nfastmap is%s accurate\n", bufp->fastmap_accurate ? "" : "n't");
  1690.   printf ("can %s be null\n----------", bufp->can_be_null ? "" : "not");
  1691. }
  1692. #endif
  1693.  
  1694. printchar (c)
  1695.      char c;
  1696. {
  1697.   if (c < 041 || c >= 0177)
  1698.     {
  1699.       putchar ('\\');
  1700.       putchar (((c >> 6) & 3) + '0');
  1701.       putchar (((c >> 3) & 7) + '0');
  1702.       putchar ((c & 7) + '0');
  1703.     }
  1704.   else
  1705.     putchar (c);
  1706. }
  1707.  
  1708. error (string)
  1709.      char *string;
  1710. {
  1711.   puts (string);
  1712.   exit (1);
  1713. }
  1714.  
  1715. #endif test
  1716.